<?php
/**
 * 插件模型 - 数据对象模型
 * @author jason <yangjs17@yeah.net>
 * @version TS3.0
 */
class ShopModel extends Model {
	//protected $tableName = 'shop';
	protected $error = '';
	protected $tableName = 'shop_product';
	var $api_url = 'http://www.cangm.com/index.php/openapi/sns/api';
	function GetShopInfo($member_id) {
		$param ['method'] = 'shop.GetShopInfo';
		$param ['member_id'] = $member_id;
		return $this->_deal ( $param );
	}
	function GetGoodsInfo($product_id) {
		$param ['method'] = 'goods.GetGoodsInfo';
		$param ['product_id'] = $product_id;
		return $this->_deal ( $param );
	}
	function GetMemberInfo($member_id, $member_type = 'seller') {
		$param ['method'] = 'member.GetMemberInfo';
		$param ['member_id'] = $member_id;
		return $this->_deal ( $param );
	}
	function UpdateToMemberInfo($member_id, $param) {
		// name
		// mobile
		// email
		// nickname
		$param ['method'] = 'member.UpdateToMemberInfo';
		$param ['member_id'] = $member_id;
		return $this->_deal ( $param );
	}
	function _deal($param) {
		$param ['api_version'] = '1.0';
		$param ['sign'] = 1;
		
		$url = $this->api_url . '?' . http_build_query ( $param );
		// dump($url);
		
		$content = file_get_contents ( $url );
		$content = json_decode ( $content, true );
		// dump($content);
		
		return $content;
	}
public function getWeibaList($limit = 20, $map = array()) {	
		if(isset($_POST)) {
			//搜索时用到
			$_POST['pid'] && $map['pid']=intval($_POST['pid']);
			$_POST['product_name'] && $map['product_name']=array('like','%'.$_POST['product_name'].'%');
			
			$_POST['shop_category_id'] && $map['shop_category_id']=intval($_POST['shop_category_id']);
			
			
			if (! empty ( $_POST ['endtime'] )) {
				if (! empty ( $_POST ['endtime'] [0] ) && ! empty ( $_POST ['endtime'] [1] )) {
					// 时间区间条件
					$map ['endtime'] = array (
							'BETWEEN',
							array (
									strtotime ( $_POST ['endtime'] [0] ),
									strtotime ( $_POST ['endtime'] [1] ) 
							) 
					);
				} else if (! empty ( $_POST ['endtime'] [0] )) {
					// 时间大于条件
					$map ['endtime'] = array (
							'GT',
							strtotime ( $_POST ['endtime'] [0] ) 
					);
				} elseif (! empty ( $_POST ['endtime'] [1] )) {
					// 时间小于条件
					$map ['endtime'] = array (
							'LT',
							strtotime ( $_POST ['endtime'] [1] ) 
					);
				}
			}	
			//dump($_POST ['credit']);exit;
			if (! empty ( $_POST ['credit'] )) {
				if (! empty ( $_POST ['credit'] [0] ) && ! empty ( $_POST ['credit'] [1] )) {
					// 时间区间条件
					$map ['credit'] = array (
							'BETWEEN',
							array (
									$_POST ['credit'] [0],
									$_POST ['credit'] [1] 
							) 
					);
				} else if (! empty ( $_POST ['credit'] [0] )) {
					// 时间大于条件
					$map ['credit'] = array (
							'GT',
							$_POST ['credit'] [0] 
					);
				} elseif (! empty ( $_POST ['credit'] [1] )) {
					// 时间小于条件
					$map ['credit'] = array (
							'LT',
							$_POST ['credit'] [1]
					);
				}
			}
			
		if($_POST['status']==1){$map['endtime']=array('LT',time());}elseif($_POST['status']==2){$map['endtime']=array('GT',time());}else{unset($map['status']);}
			
		}		
		//dump($map);exit;
		//$map['is_del'] = 0;
		// 查询数据
		$list = $this->where($map)->order("pid desc")->findPage($limit);
		//dump($list['data']);exit;
		// 数据组装
		foreach($list['data'] as $k => $v) {
			$list['data'][$k]['product_name'] = $v['product_name'];
			$logo = D('attach')->where('attach_id='.$v['product_ico'])->find();
			$list['data'][$k]['product_ico'] = '<img src="'.UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'].'" width="50" height="50">';
			$list['data'][$k]['DOACTION'] = '<a href="'.U('admin/Shop/editshop',array('pid'=>$v['pid'],'tabHash'=>'editshop')).'">编辑</a>|<a onclick="admin.delshop('.$v['pid'].');" href="javascript:void(0)">删除</a>';
			//'shop_id','product_name','logo','credit','people','end_time','DOACTION'
			$list['data'][$k]['product_name'] = $v['product_name'];
			//$list['data'][$k]['logo'] &&  $list['data'][$k]['logo'] = '<img src="'.UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'].'" width="50" height="50">';
			$list['data'][$k]['credit_type'] = $this->get_credit_type($v['credit_type']);
			$list['data'][$k]['credit'] = $v['credit'];
			$cname = M('Shop_category')->where('shop_category_id='.$v['shop_category_id'])->find();
			$list['data'][$k]['shop_category_id'] = $cname['title'];
			$list['data'][$k]['people'] = $v['people'];
			$list['data'][$k]['endtime'] = date('Y-m-d h:i:s',$v['endtime']);
			$list['data'][$k]['status'] = time()>$v['endtime']?'已过期':'出售中';
		}
		//dump($list);exit;
		return $list;
	}
	
	public function getCategory($limit = 20, $map = array()) {	
			
		//dump($map);exit;
		//$map['status'] = 1;
		// 查询数据
		$list = M('Shop_category')->where($map)->findPage($limit);
		
		// 数据组装
		foreach($list['data'] as $k => $v) {
			$list['data'][$k]['shop_category_id'] = $v['shop_category_id'];
			$list['data'][$k]['cname'] = $v['cname'];
			$list['data'][$k]['DOACTION'] = '<a href="'.U('admin/Shop/editCategory',array('shop_category_id'=>$v['cid'],'tabHash'=>'editCategory')).'">编辑</a>- <a onclick="admin.delCategory('.$v['cid'].');" href="javascript:void(0)">删除</a>- ';
			$list['data'][$k]['DOACTION'] .= $v['status'] == 0 ? '<a href="javascript:void(0)" onclick="admin.rebackCategory(\''.$v['shop_category_id'].'\')">'.'启用'.'</a>' : '<a href="javascript:void(0)" onclick="admin.disCategory(\''.$v['shop_category_id'].'\')">'.'关闭'.'</a>';
			$list['data'][$k]['description'] = $v['description'];
			$list['data'][$k]['status'] = $v['status']==1?'开启':'关闭';
			
		}
		return $list;
	}
	
	public function getconvertList($limit = 20, $map = array()) {	
		if(isset($_POST)) {
			//搜索时用到
			$_POST['weiba_id'] && $map['weiba_id']=intval($_POST['weiba_id']);
			$_POST['weiba_name'] && $map['weiba_name']=array('like','%'.$_POST['weiba_name'].'%');
			$_POST['uid'] && $map['uid']=intval($_POST['uid']);
			$_POST['admin_uid'] && $map['admin_uid']=intval($_POST['admin_uid']);
			$_POST['recommend'] && $map['recommend']=$_POST['recommend']==1?1:0;
		}		
		$map['is_del'] = 0;
		// 查询数据
		$list = $this->findPage($limit);
		
		return $list;
	}




	/**
	 * 根据微吧ID获取微吧信息
	 * @param integer $weiba_id 微吧ID
	 * @return array 微吧信息
	 */
	public function getshopById($weiba_id){
		$weiba = $this->where('pid='.$weiba_id)->find();
		if($weiba['logo']){
			$logo = D('attach')->where('attach_id='.$weiba['logo'])->find();
			$weiba['pic_url'] = UPLOAD_URL.'/'.$logo['save_path'].$logo['save_name'];
		}
		if($weiba['endtime']){
			$weiba['endtime']=date('Y-m-d h:i:s',$weiba['endtime']);
		}

		return $weiba;
	}
	
	public function get_credit_type($credit_type){
		$credit_name = D('credit_type')->where('name = "'.$credit_type.'"')->find();
		return $credit_name['alias'];
	}
}
